package com.example.myapplication.activity;

import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.database.User;
import com.example.myapplication.event.ChangeNameEvent;
import com.example.myapplication.event.PhotoResultEvent;
import com.example.myapplication.util.ActivityStack;
import com.example.myapplication.util.DbUtils;
import com.example.myapplication.util.GlideUtil;
import com.example.myapplication.window.ChooseCameraPop;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class UserInfoAty extends BaseActivity {
    private ImageView userIcon;
    private TextView userName;
    private View btIcon,btName,loRoot,btSave;
    @Override
    protected void logicStart() {
        GlideUtil.loadHead(DbUtils.getLoginUser().getAva(),userIcon);
        userName.setText(DbUtils.getLoginUser().getName());

    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        userIcon=findViewById(R.id.userIcon);
        userName=findViewById(R.id.userName);
        btIcon=findViewById(R.id.btUserIcon);
        btName=findViewById(R.id.btUserName);
        loRoot=findViewById(R.id.loFather);
        btSave=findViewById(R.id.btSave);

    }

    @Override
    protected void setListener() {
        super.setListener();
        btName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityStack.INSTANCE.startTo(UserInfoAty.this,ChangeNameAty.class,null);
            }
        });
        btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User loginUser = DbUtils.getLoginUser();
                loginUser.setName(userName.getText().toString());
                if (userIcon.getTag()!=null){
                    loginUser.setAva(userIcon.getTag().toString());
                }
                loginUser.save();

                ActivityStack.INSTANCE.popOut(UserInfoAty.this,true);
            }
        });
        btIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ChooseCameraPop(UserInfoAty.this).showAtLocation(loRoot, Gravity.BOTTOM,0,0);

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getPhoto(PhotoResultEvent event){
        userIcon.setTag(event.getPhotos().get(0).path);
        GlideUtil.loadHead(event.getPhotos().get(0).path,userIcon);
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getPhoto(ChangeNameEvent event){
        userName.setText(event.getName());

    }
    @Override
    public int getLayoutId() {
        return R.layout.aty_userinfo;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
